<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
			<el-form-item label="商品名称" prop="goods_name">
				<el-input v-model="form.goods_name" placeholder="请输入商品名称" clearable></el-input>
			</el-form-item>
            <el-form-item label="分类名称" prop="cate_id">
				<el-cascader v-model="form.cate_id" :options="cateList" :props="cateProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
			</el-form-item>
            <el-form-item label="规格" prop="spec">
				<el-input v-model="form.spec" placeholder="请输入规格" clearable></el-input>
			</el-form-item>
            <el-form-item label="单位" prop="unit">
				<el-input v-model="form.unit" placeholder="请输入单位" clearable></el-input>
			</el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input-number v-model="form.sort" :min="0" :max="9999" />
			</el-form-item>
            <el-form-item label="供应商" prop="supplier_id">
				<!-- <el-input v-model="applyName" placeholder="请选择供应商" readonly @click="checkApply"></el-input> -->
                <el-select v-model="form.supplier_id" placeholder="请选择供应商" clearable filterable multiple collapse-tags collapse-tags-tooltip :max-collapse-tags="3">
                    <el-option v-for="item in applyList" :key="item.id" :label="item.supplier_name" :value="item.id"/>
                </el-select>
			</el-form-item>
            <el-form-item label="供应商报价" prop="purchase_price">
				<el-input v-model="form.purchase_price" placeholder="请输入供应商报价" clearable></el-input>
			</el-form-item>
			<el-form-item label="创建时间" prop="date">
                <el-date-picker v-model="date" type="date" style="width: 100%;" :value-format="'YYYY-MM-DD'" disabled></el-date-picker>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <select-apply v-if="dialog.apply" ref="applyDialog" @success="applyBack" @closed="dialog.apply = false"></select-apply>
</template>

<script>
import selectApply from '@/views/supplier/list/selectApply.vue';
	export default {
  components: { selectApply },
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:"",
					goods_name: "",
					cate_id: "",
					spec: "",
					unit: "",
					purchase_price: "",
					supplier_id: "",
                    sort: "",
				},
                date:this.moment().format('YYYY-MM-DD'),
                applyName:"",
				//验证规则
				// rules: {
				// 	supplier_id: [
				// 		{required: true, message: '请选择供应商', trigger: 'change'}
				// 	],
				// },
				//所需数据选项
				cateList: [],
				cateProps: {
					value: "id",
					emitPath: false,
					checkStrictly: true
				},
                dialog:{
                    apply:false
                },
                applyList:[],
			}
		},
		mounted() {
			this.getCate()
            this.getApply()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//获取商品分类
			async getCate(){
				var res = await this.$API.product.cate.list.get();
				this.cateList = res.data;
			},
            // 供应商
            async getApply(){
                var res = await this.$API.apply.apply.all.get();
                this.applyList = res.data;
            },
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						if (this.form.id) {
							res = await this.$API.product.list.edit.post(this.form);
						}else{
							res = await this.$API.product.list.add.post(this.form);
						}
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.goods_name = data.goods_name
				this.form.spec = data.spec
				this.form.sort = data.sort
				this.form.unit = data.unit
				this.form.purchase_price = data.purchase_price
				this.form.cate_id = data.cate_id
                let a = data.supplier_id.split(",")
                this.form.supplier_id = a.map(item=>Number(item))
                this.date = data.create_time
                this.applyName = data.supplier_name
			},
            checkApply(){
                this.dialog.apply = true
                this.$nextTick(() => {
                    this.$refs.applyDialog.open()
                })
            },
            applyBack(data){
                this.form.supplier_id = data.id
                this.applyName = data.supplier_name
            },
		}
	}
</script>

<style>
</style>
